<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","version":"8.3.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="Node.jsUrl.parse( )const url&#x3D;require(&amp;#x27;url&amp;#x27;);var api&#x3D;&amp;#x27;http:&#x2F;&#x2F;www.itying.com?name&#x3D;zhangsan&amp;age&#x3D;20&amp;#x27;;var getValue&#x3D;url.parse(api,true).query;console.log(&#96;姓名:$&amp;#123;getValue.name&amp;#12">
<meta property="og:type" content="article">
<meta property="og:title" content="Node.js技术总结">
<meta property="og:url" content="http://example.com/2021/01/25/Node.js/index.html">
<meta property="og:site_name" content="Lawted&#39;s Blog">
<meta property="og:description" content="Node.jsUrl.parse( )const url&#x3D;require(&amp;#x27;url&amp;#x27;);var api&#x3D;&amp;#x27;http:&#x2F;&#x2F;www.itying.com?name&#x3D;zhangsan&amp;age&#x3D;20&amp;#x27;;var getValue&#x3D;url.parse(api,true).query;console.log(&#96;姓名:$&amp;#123;getValue.name&amp;#12">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-01-25T04:58:39.000Z">
<meta property="article:modified_time" content="2021-02-02T02:34:26.986Z">
<meta property="article:author" content="Lawted Wu">
<meta property="article:tag" content="技术">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2021/01/25/Node.js/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>
<title>Node.js技术总结 | Lawted's Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Lawted's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Node-js"><span class="nav-number">1.</span> <span class="nav-text">Node.js</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Url-parse"><span class="nav-number">1.0.1.</span> <span class="nav-text">Url.parse( )</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Node-js%E6%A8%A1%E5%9D%97"><span class="nav-number">1.0.2.</span> <span class="nav-text">Node.js模块</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#npm-%E5%91%BD%E4%BB%A4%E8%AF%A6%E8%A7%A3"><span class="nav-number">1.1.</span> <span class="nav-text">npm 命令详解</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#fs%E6%A8%A1%E5%9D%97"><span class="nav-number">1.2.</span> <span class="nav-text">fs模块</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-stat%E6%A3%80%E6%B5%8B%E6%98%AF%E7%9B%AE%E5%BD%95%E8%BF%98%E6%98%AF%E6%96%87%E4%BB%B6"><span class="nav-number">1.2.1.</span> <span class="nav-text">fs.stat检测是目录还是文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-mkdir%E5%88%9B%E5%BB%BA%E7%9B%AE%E5%BD%95"><span class="nav-number">1.2.2.</span> <span class="nav-text">fs.mkdir创建目录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-writeFile%E5%88%9B%E5%BB%BA%E6%96%87%E4%BB%B6"><span class="nav-number">1.2.3.</span> <span class="nav-text">fs.writeFile创建文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-appendFile%E8%BF%BD%E5%8A%A0%E6%96%87%E4%BB%B6"><span class="nav-number">1.2.4.</span> <span class="nav-text">fs.appendFile追加文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-readFile%E8%AF%BB%E5%8F%96%E6%96%87%E4%BB%B6"><span class="nav-number">1.2.5.</span> <span class="nav-text">fs.readFile读取文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-readdir%E8%AF%BB%E5%8F%96%E7%9B%AE%E5%BD%95"><span class="nav-number">1.2.6.</span> <span class="nav-text">fs.readdir读取目录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-rename%E9%87%8D%E5%91%BD%E5%90%8D"><span class="nav-number">1.2.7.</span> <span class="nav-text">fs.rename重命名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-rmdir%E5%88%A0%E9%99%A4%E7%9B%AE%E5%BD%95"><span class="nav-number">1.2.8.</span> <span class="nav-text">fs.rmdir删除目录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-unlink%E5%88%A0%E9%99%A4%E6%96%87%E4%BB%B6"><span class="nav-number">1.2.9.</span> <span class="nav-text">fs.unlink删除文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A8%A1%E6%9D%BF%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-number">1.2.10.</span> <span class="nav-text">模板字符串</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0"><span class="nav-number">1.2.11.</span> <span class="nav-text">箭头函数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Promise"><span class="nav-number">1.3.</span> <span class="nav-text">Promise</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#fs%E4%B8%AD%E7%9A%84%E6%B5%81%E4%BB%A5%E5%8F%8A%E7%AE%A1%E9%81%93%E6%B5%81"><span class="nav-number">2.</span> <span class="nav-text">fs中的流以及管道流</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-createReadStream-%E4%BB%8E%E6%96%87%E4%BB%B6%E6%B5%81%E4%B8%AD%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE"><span class="nav-number">2.0.1.</span> <span class="nav-text">fs.createReadStream 从文件流中读取数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-createWriteStream%E5%90%91%E6%96%87%E4%BB%B6%E4%B8%AD%E5%86%99%E5%85%A5%E6%96%87%E4%BB%B6"><span class="nav-number">2.0.2.</span> <span class="nav-text">fs.createWriteStream向文件中写入文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E7%AE%A1%E9%81%93%E6%B5%81%E8%BF%9B%E8%A1%8C%E5%A4%A7%E6%96%87%E4%BB%B6%E7%9A%84%E5%A4%8D%E5%88%B6"><span class="nav-number">2.0.3.</span> <span class="nav-text">使用管道流进行大文件的复制</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#MongoDB%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="nav-number">3.</span> <span class="nav-text">MongoDB数据库</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="nav-number">3.0.1.</span> <span class="nav-text">基本操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E8%AF%A2"><span class="nav-number">3.0.2.</span> <span class="nav-text">查询</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BF%AE%E6%94%B9"><span class="nav-number">3.0.3.</span> <span class="nav-text">修改</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%A0%E9%99%A4"><span class="nav-number">3.0.4.</span> <span class="nav-text">删除</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%8E%B7%E5%8F%96%E6%9F%A5%E8%AF%A2%E6%97%B6%E9%97%B4"><span class="nav-number">3.0.5.</span> <span class="nav-text">获取查询时间</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B4%A2%E5%BC%95%E5%9F%BA%E7%A1%80"><span class="nav-number">3.0.6.</span> <span class="nav-text">索引基础</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A4%8D%E5%90%88%E7%B4%A2%E5%BC%95"><span class="nav-number">3.0.6.1.</span> <span class="nav-text">复合索引</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%94%AF%E4%B8%80%E7%B4%A2%E5%BC%95"><span class="nav-number">3.0.6.2.</span> <span class="nav-text">唯一索引</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#MongoDB%E8%B4%A6%E6%88%B7%E6%9D%83%E9%99%90%E9%85%8D%E7%BD%AE"><span class="nav-number">3.1.</span> <span class="nav-text">MongoDB账户权限配置</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#node-js-%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="nav-number">4.</span> <span class="nav-text">node.js 连接数据库</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E6%89%BE%E6%95%B0%E6%8D%AE"><span class="nav-number">4.0.1.</span> <span class="nav-text">查找数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A2%9E%E5%8A%A0%E6%95%B0%E6%8D%AE"><span class="nav-number">4.0.2.</span> <span class="nav-text">增加数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BF%AE%E6%94%B9%E6%95%B0%E6%8D%AE"><span class="nav-number">4.0.3.</span> <span class="nav-text">修改数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE"><span class="nav-number">4.0.4.</span> <span class="nav-text">删除数据</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Express"><span class="nav-number">5.</span> <span class="nav-text">Express</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="nav-number">5.1.</span> <span class="nav-text">基本使用</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AE%89%E8%A3%85express"><span class="nav-number">5.1.0.1.</span> <span class="nav-text">安装express</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#get%EF%BC%8Cpost%EF%BC%8Cput%EF%BC%8Cdelete"><span class="nav-number">5.1.0.2.</span> <span class="nav-text">get，post，put，delete</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A4%9A%E7%BA%A7%E8%B7%AF%E7%94%B1"><span class="nav-number">5.1.0.3.</span> <span class="nav-text">多级路由</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1"><span class="nav-number">5.1.0.4.</span> <span class="nav-text">动态路由</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#get%E4%BC%A0%E5%80%BC"><span class="nav-number">5.1.1.</span> <span class="nav-text">get传值</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ejs"><span class="nav-number">5.2.</span> <span class="nav-text">ejs</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AE%89%E8%A3%85ejs"><span class="nav-number">5.2.0.1.</span> <span class="nav-text">安装ejs</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#ejs%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="nav-number">5.2.0.2.</span> <span class="nav-text">ejs基本使用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#ejs-%E8%BF%9B%E9%98%B6%E4%BD%BF%E7%94%A8"><span class="nav-number">5.2.0.3.</span> <span class="nav-text">ejs 进阶使用</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#express-%E4%B8%AD%E9%97%B4%E4%BB%B6"><span class="nav-number">5.3.</span> <span class="nav-text">express 中间件</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%BA%94%E7%94%A8%E7%BA%A7%E4%B8%AD%E9%97%B4%E4%BB%B6"><span class="nav-number">5.3.0.1.</span> <span class="nav-text">应用级中间件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%B7%AF%E7%94%B1%E4%B8%AD%E9%97%B4%E4%BB%B6"><span class="nav-number">5.3.0.2.</span> <span class="nav-text">路由中间件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86%E4%B8%AD%E9%97%B4%E4%BB%B6"><span class="nav-number">5.3.0.3.</span> <span class="nav-text">错误处理中间件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%86%85%E7%BD%AE%E4%B8%AD%E9%97%B4%E4%BB%B6-%E9%9D%99%E6%80%81"><span class="nav-number">5.3.0.4.</span> <span class="nav-text">内置中间件(静态)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%AC%AC%E4%B8%89%E6%96%B9%E4%B8%AD%E9%97%B4%E4%BB%B6"><span class="nav-number">5.3.0.5.</span> <span class="nav-text">第三方中间件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#express-router"><span class="nav-number">5.3.0.6.</span> <span class="nav-text">express.router</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#express%E5%92%8Cmongoose"><span class="nav-number">5.4.</span> <span class="nav-text">express和mongoose</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9F%A5%E8%AF%A2users%E8%A1%A8%E7%9A%84%E6%95%B0%E6%8D%AE"><span class="nav-number">5.4.0.1.</span> <span class="nav-text">查询users表的数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A2%9E%E5%8A%A0%E6%95%B0%E6%8D%AE-1"><span class="nav-number">5.4.0.2.</span> <span class="nav-text">增加数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9B%B4%E6%96%B0%E6%95%B0%E6%8D%AE"><span class="nav-number">5.4.0.3.</span> <span class="nav-text">更新数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE-1"><span class="nav-number">5.4.0.4.</span> <span class="nav-text">删除数据</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A8%A1%E5%9D%97%E5%8C%96"><span class="nav-number">5.4.1.</span> <span class="nav-text">模块化</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Nginx%E9%85%8D%E7%BD%AE"><span class="nav-number">5.5.</span> <span class="nav-text">Nginx配置</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Lawted Wu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">67</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/LAWTED" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;LAWTED" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="Back to top">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/01/25/Node.js/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Node.js技术总结
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-01-25 12:58:39" itemprop="dateCreated datePublished" datetime="2021-01-25T12:58:39+08:00">2021-01-25</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-02-02 10:34:26" itemprop="dateModified" datetime="2021-02-02T10:34:26+08:00">2021-02-02</time>
      </span>

  
    <span class="post-meta-item" title="Views" id="busuanzi_container_page_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">Views: </span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Node-js"><a href="#Node-js" class="headerlink" title="Node.js"></a>Node.js</h1><h3 id="Url-parse"><a href="#Url-parse" class="headerlink" title="Url.parse( )"></a>Url.parse( )</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> url=<span class="built_in">require</span>(<span class="string">&#x27;url&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> api=<span class="string">&#x27;http://www.itying.com?name=zhangsan&amp;age=20&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> getValue=url.parse(api,<span class="literal">true</span>).query;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">`姓名:<span class="subst">$&#123;getValue.name&#125;</span>--年龄:<span class="subst">$&#123;getValue.age&#125;</span>`</span>);</span><br></pre></td></tr></table></figure>
<h3 id="Node-js模块"><a href="#Node-js模块" class="headerlink" title="Node.js模块"></a>Node.js模块</h3><ul>
<li>exports</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//tools.js</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">formatApi</span>(<span class="params">api</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="string">&quot;http://www.itying.com/&quot;</span>+api;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">exports</span>.formatApi=formatApi;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> http = <span class="built_in">require</span>(<span class="string">&#x27;http&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> tools=<span class="built_in">require</span>(<span class="string">&#x27;./module/tools.js&#x27;</span>);</span><br><span class="line"></span><br><span class="line">http.createServer(<span class="function"><span class="keyword">function</span> (<span class="params">request, response</span>) </span>&#123;</span><br><span class="line">  response.writeHead(<span class="number">200</span>, &#123;<span class="string">&#x27;Content-Type&#x27;</span>: <span class="string">&#x27;text/html&#x27;</span>&#125;);</span><br><span class="line">  response.write(<span class="string">&quot;&lt;head&gt;&lt;meta charset=&#x27;UTF-8&#x27;&gt;&lt;/head&gt;&quot;</span>)</span><br><span class="line">  <span class="keyword">var</span> api=tools.formatApi(<span class="string">&#x27;api/plist&#x27;</span>);<span class="comment">//在方法前面加上文件的名字.</span></span><br><span class="line">  response.write(api);</span><br><span class="line">  response.end();</span><br><span class="line">&#125;).listen(<span class="number">8081</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;Server running at http://127.0.0.1:8081/&#x27;</span>);</span><br></pre></td></tr></table></figure>
<ul>
<li>module.exports</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//r1equest.js</span></span><br><span class="line"><span class="keyword">var</span> obj=&#123;</span><br><span class="line">  get:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;从服务器获取数据&#x27;</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">  post:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;提交数据&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports=obj;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> request=<span class="built_in">require</span>(<span class="string">&#x27;./module/request&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(request);</span><br></pre></td></tr></table></figure>
<p>如果在node_modules 文件夹下，路径可以直接写文件名</p>
<p><strong>使用npm init –yes生成package.json</strong></p>
<h2 id="npm-命令详解"><a href="#npm-命令详解" class="headerlink" title="npm 命令详解"></a>npm 命令详解</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">npm -v   					查看npm版本</span><br><span class="line">npm install Module_Name 	安装模块</span><br><span class="line">npm uninstall Module_Name 	卸载模块</span><br><span class="line">npm list 					查看当前目录下已安装的node包</span><br><span class="line">npm info Module_Name 		查看模块的版本</span><br><span class="line">npm install Module_Name@1.80安装指定版本的模块</span><br></pre></td></tr></table></figure>


<h2 id="fs模块"><a href="#fs模块" class="headerlink" title="fs模块"></a>fs模块</h2><h3 id="fs-stat检测是目录还是文件"><a href="#fs-stat检测是目录还是文件" class="headerlink" title="fs.stat检测是目录还是文件"></a>fs.stat检测是目录还是文件</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"></span><br><span class="line">fs.stat(<span class="string">&#x27;./app.js&#x27;</span>,<span class="function">(<span class="params">err,data</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="keyword">return</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">`是文件:<span class="subst">$&#123;data.isFile()&#125;</span>`</span>);</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">`是目录:<span class="subst">$&#123;data.isDirectory()&#125;</span>`</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h3 id="fs-mkdir创建目录"><a href="#fs-mkdir创建目录" class="headerlink" title="fs.mkdir创建目录"></a>fs.mkdir创建目录</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"></span><br><span class="line">fs.mkdir(<span class="string">&#x27;./css&#x27;</span>,<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="keyword">return</span> ;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;创建成功&#x27;</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h3 id="fs-writeFile创建文件"><a href="#fs-writeFile创建文件" class="headerlink" title="fs.writeFile创建文件"></a>fs.writeFile创建文件</h3><p><strong>如果已经有会覆盖重写</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"></span><br><span class="line">fs.writeFile(<span class="string">&#x27;./html5/index.html&#x27;</span>,<span class="string">&#x27;你好node.js&#x27;</span>,<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="keyword">return</span> ;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;创建成功&#x27;</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h3 id="fs-appendFile追加文件"><a href="#fs-appendFile追加文件" class="headerlink" title="fs.appendFile追加文件"></a>fs.appendFile追加文件</h3><p><strong>如果没有会创建，如果有会追加</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"></span><br><span class="line">fs.appendFile(<span class="string">&#x27;./css/base.css&#x27;</span>,<span class="string">&#x27;body&#123;color:red&#125;&#x27;</span>,<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="keyword">return</span> ;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;appendFile 成功&#x27;</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h3 id="fs-readFile读取文件"><a href="#fs-readFile读取文件" class="headerlink" title="fs.readFile读取文件"></a>fs.readFile读取文件</h3><p>默认是buffer文件，需要转换为string</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"></span><br><span class="line">fs.readFile(<span class="string">&#x27;./html5/index.html&#x27;</span>,<span class="function">(<span class="params">err,data</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(data);<span class="comment">//这是一个buffer文件</span></span><br><span class="line">  <span class="built_in">console</span>.log(data.toString());</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h3 id="fs-readdir读取目录"><a href="#fs-readdir读取目录" class="headerlink" title="fs.readdir读取目录"></a>fs.readdir读取目录</h3><p>读取目录下的文件，也可以读取目录下的文件夹</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"></span><br><span class="line">fs.readdir(<span class="string">&#x27;./html5&#x27;</span>,<span class="function">(<span class="params">err,data</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(data);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h3 id="fs-rename重命名"><a href="#fs-rename重命名" class="headerlink" title="fs.rename重命名"></a>fs.rename重命名</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"></span><br><span class="line">fs.rename(<span class="string">&#x27;./css/base.css&#x27;</span>,<span class="string">&#x27;./css/index.css&#x27;</span>,<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="keyword">return</span> ;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;重命名完成&#x27;</span>);</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">同时他还能执行移动操作</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"></span><br><span class="line">fs.rename(<span class="string">&#x27;./css/index.css&#x27;</span>,<span class="string">&#x27;./html5/base.css&#x27;</span>,<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="keyword">return</span> ;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;移动完成&#x27;</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h3 id="fs-rmdir删除目录"><a href="#fs-rmdir删除目录" class="headerlink" title="fs.rmdir删除目录"></a>fs.rmdir删除目录</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"></span><br><span class="line">fs.rmdir(<span class="string">&#x27;./html6&#x27;</span>,<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;删除目录成功&#x27;</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h3 id="fs-unlink删除文件"><a href="#fs-unlink删除文件" class="headerlink" title="fs.unlink删除文件"></a>fs.unlink删除文件</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"></span><br><span class="line">fs.unlink(<span class="string">&#x27;./html5/html6/del.html&#x27;</span>,<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;删除成功&#x27;</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h3 id="模板字符串"><a href="#模板字符串" class="headerlink" title="模板字符串"></a>模板字符串</h3><p>注意不是‘ ’是``</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> name=<span class="string">&#x27;张三&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> age=<span class="number">20</span>;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">`<span class="subst">$&#123;name&#125;</span>的年龄是<span class="subst">$&#123;age&#125;</span>`</span>);</span><br></pre></td></tr></table></figure>


<h3 id="箭头函数"><a href="#箭头函数" class="headerlink" title="箭头函数"></a>箭头函数</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> team=&#123;</span><br><span class="line"></span><br><span class="line">      name:[<span class="string">&#x27;tom&#x27;</span>,<span class="string">&#x27;bob&#x27;</span>],</span><br><span class="line">      teamName:<span class="string">&#x27;ES6&#x27;</span>,</span><br><span class="line">      teamSummary:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">          <span class="keyword">return</span> <span class="built_in">this</span>.name.map(<span class="function">(<span class="params">items</span>)=&gt;</span> &#123;</span><br><span class="line">          <span class="comment">//当前this已经没有明确指向了,如果要明确，就得在上一个return之前先声明一个this；</span></span><br><span class="line">          <span class="keyword">return</span> <span class="string">`<span class="subst">$&#123;items&#125;</span>属于<span class="subst">$&#123;<span class="built_in">this</span>.teamName&#125;</span>分类下`</span></span><br><span class="line">          &#125;)</span><br><span class="line">      &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>


<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//普通写法</span></span><br><span class="line">    <span class="keyword">const</span> number=<span class="function"><span class="keyword">function</span>(<span class="params">number</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> number*<span class="number">2</span></span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//箭头函数</span></span><br><span class="line">    <span class="keyword">const</span> number2=<span class="function">(<span class="params">number</span>)=&gt;</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> number*<span class="number">2</span></span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">console</span>.log(number2(<span class="number">2</span>)) <span class="comment">//--&gt;4</span></span><br></pre></td></tr></table></figure>


<h2 id="Promise"><a href="#Promise" class="headerlink" title="Promise"></a>Promise</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> promise = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve, reject</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 异步处理</span></span><br><span class="line">    <span class="comment">// 处理结束后、调用resolve 或 reject</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>Promise 构造函数包含一个参数和一个带有 resolve（解析）和 reject（拒绝）两个参数的回调。在回调中执行一些操作（例如异步），如果一切都正常，则调用 resolve，否则调用 reject</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">promise.then(onFulfilled).catch(onRejected)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> p=<span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve,reject</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">setTimeout</span>(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> name=<span class="string">&#x27;张三&#x27;</span>;</span><br><span class="line">    resolve(name);</span><br><span class="line">  &#125;,<span class="number">1000</span>);</span><br><span class="line">&#125;)</span><br><span class="line">p.then(<span class="function"><span class="keyword">function</span>(<span class="params">data</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(data);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<p>async是异步的简写，而await可以认为是async wait的简写。async用于申明一个异步的function,await用于等待一个异步方法执行完成</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">test</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="string">&#x27;你好test&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(test());<span class="comment">//Promise &#123; &#x27;你好test&#x27; &#125;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//await必须得用在async的方法里面</span></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">test</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve,reject</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">      <span class="keyword">var</span> name=<span class="string">&#x27;张三 232&#x27;</span>;</span><br><span class="line">      resolve(name);</span><br><span class="line">    &#125;,<span class="number">1000</span>);</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">main</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="keyword">var</span> data=<span class="keyword">await</span> test();</span><br><span class="line">  <span class="built_in">console</span>.log(data);</span><br><span class="line">&#125;</span><br><span class="line">main();</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//寻找wwwroot下的目录，并把目录放入一个数组中</span></span><br><span class="line"><span class="keyword">const</span> &#123; rejects &#125; = <span class="built_in">require</span>(<span class="string">&#x27;assert&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> path=<span class="string">&#x27;./wwwroot&#x27;</span></span><br><span class="line"><span class="keyword">var</span> dirArr=[];</span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">isDir</span>(<span class="params">path</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve,rejects</span>)=&gt;</span>&#123;</span><br><span class="line">    fs.stat(path,<span class="function">(<span class="params">err,stats</span>)=&gt;</span>&#123;</span><br><span class="line">      <span class="keyword">if</span>(err)&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(err);</span><br><span class="line">        rejects(err);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">if</span>(stats.isDirectory())&#123;</span><br><span class="line">        resolve(<span class="literal">true</span>);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">else</span>&#123;</span><br><span class="line">        resolve(<span class="literal">false</span>);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;<span class="comment">//封装异步的isDir</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">main</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  fs.readdir(path,<span class="keyword">async</span> (err,data)=&gt;&#123;</span><br><span class="line">    <span class="keyword">if</span>(err)&#123;</span><br><span class="line">      <span class="built_in">console</span>.log(err);</span><br><span class="line">      <span class="keyword">return</span> ;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="number">0</span>;i&lt;data.length;i++)&#123;</span><br><span class="line">      <span class="keyword">if</span>(<span class="keyword">await</span> isDir(path+<span class="string">&#x27;/&#x27;</span>+data[i]))&#123;<span class="comment">//for循环内部使用await判断</span></span><br><span class="line">        dirArr.push(data[i]);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">console</span>.log(dirArr);</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">main();</span><br></pre></td></tr></table></figure>
<h1 id="fs中的流以及管道流"><a href="#fs中的流以及管道流" class="headerlink" title="fs中的流以及管道流"></a>fs中的流以及管道流</h1><h3 id="fs-createReadStream-从文件流中读取数据"><a href="#fs-createReadStream-从文件流中读取数据" class="headerlink" title="fs.createReadStream 从文件流中读取数据"></a>fs.createReadStream 从文件流中读取数据</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> readStream=fs.createReadStream(<span class="string">&#x27;./data/input.txt&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> count=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">var</span> str=<span class="string">&#x27; &#x27;</span>;</span><br><span class="line">readStream.on(<span class="string">&#x27;data&#x27;</span>,<span class="function">(<span class="params">data</span>)=&gt;</span>&#123;</span><br><span class="line">  str+=data;</span><br><span class="line">  count++;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">readStream.on(<span class="string">&#x27;end&#x27;</span>,<span class="function">()=&gt;</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(str);</span><br><span class="line">  <span class="built_in">console</span>.log(count);</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">readStream.on(<span class="string">&#x27;error&#x27;</span>,<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(err);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h3 id="fs-createWriteStream向文件中写入文件"><a href="#fs-createWriteStream向文件中写入文件" class="headerlink" title="fs.createWriteStream向文件中写入文件"></a>fs.createWriteStream向文件中写入文件</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> str=<span class="string">&#x27;&#x27;</span>;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="number">0</span>;i&lt;<span class="number">500</span>;i++)&#123;</span><br><span class="line">  str+=<span class="string">&#x27;我是从数据库获取的数据\n&#x27;</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> writeStream=fs.createWriteStream(<span class="string">&#x27;./data/outpu.txt&#x27;</span>);</span><br><span class="line">writeStream.write(str);</span><br><span class="line"></span><br><span class="line"><span class="comment">//标记写入完成</span></span><br><span class="line">writeStream.end();</span><br><span class="line"></span><br><span class="line">writeStream.on(<span class="string">&#x27;finish&#x27;</span>,<span class="function">()=&gt;</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;写入完成&#x27;</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h3 id="使用管道流进行大文件的复制"><a href="#使用管道流进行大文件的复制" class="headerlink" title="使用管道流进行大文件的复制"></a>使用管道流进行大文件的复制</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> readStream=fs.createReadStream(<span class="string">&#x27;./data/aaa.jpg&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> writeSream=fs.createWriteStream(<span class="string">&#x27;./bbb.jpg&#x27;</span>);</span><br><span class="line">readStream.pipe(writeSream);</span><br></pre></td></tr></table></figure>


<h1 id="MongoDB数据库"><a href="#MongoDB数据库" class="headerlink" title="MongoDB数据库"></a>MongoDB数据库</h1><h3 id="基本操作"><a href="#基本操作" class="headerlink" title="基本操作"></a>基本操作</h3><ul>
<li>开始</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">mongo</span><br></pre></td></tr></table></figure>
<ul>
<li>使用数据库，创建数据库</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">use itying</span><br></pre></td></tr></table></figure>
<ul>
<li>向数据库的表中插入数据</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.表名.insert(&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;zhangsan&quot;</span>&#125;);</span><br></pre></td></tr></table></figure>
<ul>
<li>查看数据库</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">show dbs</span><br></pre></td></tr></table></figure>
<ul>
<li>查看数据库中有哪些表</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">show collections</span><br></pre></td></tr></table></figure>
<ul>
<li>查看表中的数据</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.find()</span><br></pre></td></tr></table></figure>
<ul>
<li>删除数据库</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.dropDatabase();</span><br></pre></td></tr></table></figure>
<ul>
<li>删除集合</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.drop();<span class="comment">//中间是表的名字</span></span><br></pre></td></tr></table></figure>
<h3 id="查询"><a href="#查询" class="headerlink" title="查询"></a>查询</h3><ul>
<li>查找age=20的数据</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.find(&#123;<span class="string">&quot;age&quot;</span>:<span class="number">20</span>&#125;);</span><br></pre></td></tr></table></figure>
<ul>
<li>查找age&gt;20的数据</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.find(&#123;<span class="string">&quot;age&quot;</span>:&#123;<span class="attr">$gt</span>:<span class="number">22</span>&#125;&#125;);<span class="comment">//大于  </span></span><br><span class="line">db.user.find(&#123;<span class="string">&quot;age&quot;</span>:&#123;<span class="attr">$gte</span>:<span class="number">22</span>&#125;&#125;);<span class="comment">//大于等于</span></span><br></pre></td></tr></table></figure>
<ul>
<li>查找age&lt;20的数据</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.find(&#123;<span class="string">&quot;age&quot;</span>:&#123;<span class="attr">$lt</span>:<span class="number">22</span>&#125;&#125;);<span class="comment">//小于</span></span><br><span class="line">db.user.find(&#123;<span class="string">&quot;age&quot;</span>:&#123;<span class="attr">$lte</span>:<span class="number">22</span>&#125;&#125;);<span class="comment">//小于等于</span></span><br></pre></td></tr></table></figure>
<ul>
<li>查询name中包含zhangsan的数据</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.find(&#123;<span class="attr">name</span>:<span class="regexp">/zhangsan/</span>&#125;);</span><br></pre></td></tr></table></figure>
<ul>
<li>查询指定列name、age数据</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.find(&#123;&#125;,&#123;<span class="attr">name</span>:<span class="number">1</span>,<span class="attr">age</span>:<span class="number">1</span>&#125;);</span><br></pre></td></tr></table></figure>
<ul>
<li>按照年龄排序  1升序 -1降序</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.find().sort(&#123;<span class="attr">age</span>:<span class="number">1</span>&#125;);</span><br><span class="line">db.user.find().sort(&#123;<span class="attr">age</span>:-<span class="number">1</span>&#125;);</span><br></pre></td></tr></table></figure>
<ul>
<li>查找前5条数据</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.find().limit(<span class="number">5</span>);</span><br></pre></td></tr></table></figure>
<ul>
<li>查找10条以后的数据</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.find().skip(<span class="number">10</span>);</span><br></pre></td></tr></table></figure>
<ul>
<li>查询数据的数量</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.find().count();</span><br></pre></td></tr></table></figure>
<ul>
<li>or查询</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.find(&#123;<span class="attr">$or</span>:[&#123;<span class="attr">age</span>:<span class="number">22</span>&#125;,&#123;<span class="attr">age</span>:<span class="number">25</span>&#125;]&#125;);</span><br></pre></td></tr></table></figure>
<hr>
<h3 id="修改"><a href="#修改" class="headerlink" title="修改"></a>修改</h3><ul>
<li>修改一个</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.admin.update(&#123;<span class="string">&quot;username&quot;</span>:<span class="string">&quot;zhangsan0&quot;</span>&#125;,&#123;<span class="attr">$set</span>:&#123;<span class="string">&quot;username&quot;</span>:<span class="string">&quot;张三&quot;</span>&#125;&#125;);</span><br></pre></td></tr></table></figure>
<ul>
<li>修改多个</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.admin.update(&#123;<span class="string">&quot;age&quot;</span>:<span class="number">101</span>&#125;,&#123;<span class="attr">$set</span>:&#123;<span class="string">&quot;username&quot;</span>:<span class="string">&quot;zhangsan1&quot;</span>&#125;&#125;,&#123;<span class="attr">multi</span>:<span class="literal">true</span>&#125;);</span><br></pre></td></tr></table></figure>
<hr>
<h3 id="删除"><a href="#删除" class="headerlink" title="删除"></a>删除</h3><ul>
<li>匹配项删除</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.admin.remove(&#123;<span class="string">&quot;username&quot;</span>:<span class="string">&quot;zhangsan1&quot;</span>&#125;);</span><br></pre></td></tr></table></figure>
<ul>
<li>只删除一个</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.admin.remove(&#123;<span class="string">&quot;username&quot;</span>:<span class="string">&quot;zhangsan1&quot;</span>&#125;,&#123;<span class="attr">justOne</span>:<span class="literal">true</span>&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="获取查询时间"><a href="#获取查询时间" class="headerlink" title="获取查询时间"></a>获取查询时间</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.find(&#123;<span class="attr">username</span>:<span class="string">&quot;zhangsan1&quot;</span>&#125;).explain(<span class="string">&quot;executionStats&quot;</span>);</span><br></pre></td></tr></table></figure>


<h3 id="索引基础"><a href="#索引基础" class="headerlink" title="索引基础"></a>索引基础</h3><ul>
<li>创建索引的命令</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.username.ensureIndex(&#123;<span class="string">&quot;username&quot;</span>:<span class="number">1</span>&#125;);</span><br></pre></td></tr></table></figure>
<p>语法中 Key 值为你要创建的索引字段，1 为指定按升序创建索引，如果你想按降序来创建索引指定为 -1 即可。</p>
<ul>
<li>获取当前集合的索引</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.getIndexes();</span><br></pre></td></tr></table></figure>
<ul>
<li>删除索引的命令</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.dropIndex(&#123;<span class="string">&quot;username&quot;</span>:<span class="number">1</span>&#125;);</span><br></pre></td></tr></table></figure>


<h4 id="复合索引"><a href="#复合索引" class="headerlink" title="复合索引"></a>复合索引</h4><ul>
<li>创建复合索引</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.user.ensureIndex(&#123;<span class="string">&quot;username&quot;</span>:<span class="number">1</span>,<span class="string">&quot;age&quot;</span>:-<span class="number">1</span>&#125;);</span><br></pre></td></tr></table></figure>
<h4 id="唯一索引"><a href="#唯一索引" class="headerlink" title="唯一索引"></a>唯一索引</h4><p>如果再次插入userid重复的文档时，MongoDB将报错，提示插入重复键</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.username.ensureIndex(&#123;<span class="string">&quot;username&quot;</span>:<span class="number">1</span>&#125;,&#123;<span class="string">&quot;unique&quot;</span>:<span class="literal">true</span>&#125;);</span><br></pre></td></tr></table></figure>


<h2 id="MongoDB账户权限配置"><a href="#MongoDB账户权限配置" class="headerlink" title="MongoDB账户权限配置"></a>MongoDB账户权限配置</h2><ul>
<li>创建超级管理用户</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">use admin</span><br><span class="line"></span><br><span class="line">db.createUser(&#123;</span><br><span class="line">    user:<span class="string">&#x27;admin&#x27;</span>,</span><br><span class="line">    pwd:<span class="string">&#x27;12345&#x27;</span>,</span><br><span class="line">    roles:[&#123;<span class="attr">role</span>:<span class="string">&#x27;root&#x27;</span>,<span class="attr">db</span>:<span class="string">&#x27;admin&#x27;</span>&#125;]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<ul>
<li>使用超级管理用户连接数据库</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">mongo admin -u admin -p <span class="number">12345</span></span><br><span class="line">mongo <span class="number">192.168</span><span class="number">.1</span><span class="number">.200</span><span class="number">.27017</span>/test -u user -p password</span><br></pre></td></tr></table></figure>
<ul>
<li>给一个数据库创建管理员</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">use itying</span><br><span class="line"></span><br><span class="line">db.createUser(&#123;</span><br><span class="line">    user:<span class="string">&quot;littleadmin&quot;</span>,</span><br><span class="line">    pwd:<span class="string">&quot;12345&quot;</span>,</span><br><span class="line">    roles:[&#123;<span class="attr">role</span>:<span class="string">&quot;dbOwner&quot;</span>,<span class="attr">db</span>:<span class="string">&#x27;itying&#x27;</span>&#125;]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<ul>
<li>使用数据库用户连接数据库</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">mongo itying -u littleadmin -p <span class="number">12345</span></span><br></pre></td></tr></table></figure>
<ul>
<li>删除数据库管理员</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.dropUser(<span class="string">&quot;littleadmin&quot;</span>);</span><br></pre></td></tr></table></figure>
<h1 id="node-js-连接数据库"><a href="#node-js-连接数据库" class="headerlink" title="node.js 连接数据库"></a>node.js 连接数据库</h1><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> MongoClient = <span class="built_in">require</span>(<span class="string">&#x27;mongodb&#x27;</span>).MongoClient;</span><br><span class="line"><span class="comment">//定义数据库连接的地址</span></span><br><span class="line"><span class="keyword">const</span> url = <span class="string">&#x27; mongodb://127.0.0.1:27017/&#x27;</span>;</span><br><span class="line"><span class="comment">//定义要操作的数据库</span></span><br><span class="line"><span class="keyword">const</span> dbName = <span class="string">&#x27;itying&#x27;</span>;</span><br><span class="line"><span class="comment">//实例化MongoClient 传入数据库连接地址</span></span><br><span class="line"><span class="keyword">const</span> client = <span class="keyword">new</span> MongoClient(url, &#123; <span class="attr">useUnifiedTopology</span>: <span class="literal">true</span> &#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">//连接数据库</span></span><br><span class="line">client.connect(<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;数据库连接成功&quot;</span>);</span><br><span class="line">  <span class="keyword">let</span> db=client.db(dbName);</span><br><span class="line">  client.close();</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="查找数据"><a href="#查找数据" class="headerlink" title="查找数据"></a>查找数据</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.collection(<span class="string">&quot;user&quot;</span>).find(&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&#x27;zhangsan&#x27;</span>&#125;).toArray(<span class="function">(<span class="params">err,data</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(data);</span><br><span class="line">  client.close();</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="增加数据"><a href="#增加数据" class="headerlink" title="增加数据"></a>增加数据</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.collection(<span class="string">&quot;user&quot;</span>).insertOne(&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;zhaosi&quot;</span>,<span class="string">&quot;age&quot;</span>:<span class="number">10</span>&#125;,<span class="function">(<span class="params">err,data</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;增加失败&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;增加成功&quot;</span>);</span><br><span class="line">  <span class="built_in">console</span>.log(data);</span><br><span class="line">  client.close();</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="修改数据"><a href="#修改数据" class="headerlink" title="修改数据"></a>修改数据</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.collection(<span class="string">&quot;user&quot;</span>).updateOne(&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;zhangsan&quot;</span>&#125;,&#123;<span class="attr">$set</span>:&#123;<span class="string">&quot;age&quot;</span>:<span class="number">10</span>&#125;&#125;,<span class="function">(<span class="params">err,data</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;修改失败&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;修改成功&quot;</span>);</span><br><span class="line">  <span class="built_in">console</span>.log(data);</span><br><span class="line">  client.close();</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="删除数据"><a href="#删除数据" class="headerlink" title="删除数据"></a>删除数据</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">db.collection(<span class="string">&quot;user&quot;</span>).deleteOne(&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;zhangsan&quot;</span>&#125;,<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;删除失败&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;删除成功&quot;</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h1 id="Express"><a href="#Express" class="headerlink" title="Express"></a>Express</h1><h2 id="基本使用"><a href="#基本使用" class="headerlink" title="基本使用"></a>基本使用</h2><h4 id="安装express"><a href="#安装express" class="headerlink" title="安装express"></a>安装express</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">npm i express --save</span><br></pre></td></tr></table></figure>
<h4 id="get，post，put，delete"><a href="#get，post，put，delete" class="headerlink" title="get，post，put，delete"></a>get，post，put，delete</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> express=<span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> app=express()</span><br><span class="line"></span><br><span class="line">app.get(<span class="string">&quot;/&quot;</span>,<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123;</span><br><span class="line">  res.send(<span class="string">&quot;你好express&quot;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.get(<span class="string">&quot;/login&quot;</span>,<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123; <span class="comment">//get:显示数据</span></span><br><span class="line">  res.send(<span class="string">&quot;登入&quot;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.post(<span class="string">&quot;/doLogin&quot;</span>,<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123; <span class="comment">//post:增加数据</span></span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;执行登入了&quot;</span>);</span><br><span class="line">  res.send(<span class="string">&quot;执行登入&quot;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.put(<span class="string">&quot;/editUser&quot;</span>,<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123; <span class="comment">//put:用于修改数据</span></span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;修改用户&quot;</span>);</span><br><span class="line">  res.send(<span class="string">&quot;修改用户&quot;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.delete(<span class="string">&quot;/deleteUser&quot;</span>,<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123; <span class="comment">//delete:用于删除数据</span></span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;执行删除&quot;</span>);</span><br><span class="line">  res.send(<span class="string">&quot;执行删除&quot;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.listen(<span class="number">3000</span>)</span><br></pre></td></tr></table></figure>


<h4 id="多级路由"><a href="#多级路由" class="headerlink" title="多级路由"></a>多级路由</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">app.get(<span class="string">&quot;/admin/user/add&quot;</span>,<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123; <span class="comment">//get:显示数据</span></span><br><span class="line">  res.send(<span class="string">&quot;admin user&quot;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h4 id="动态路由"><a href="#动态路由" class="headerlink" title="动态路由"></a>动态路由</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">app.get(<span class="string">&quot;/article/:id&quot;</span>,<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123; <span class="comment">//get:显示数据</span></span><br><span class="line">  <span class="keyword">var</span> id=req.params[<span class="string">&quot;id&quot;</span>];</span><br><span class="line">  res.send(<span class="string">&quot;动态路由+&quot;</span>+id)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<p><strong>配置路由的时候需要注意顺序，多级路由和动态路由，如果多级路由在后面，会自动匹配成id</strong></p>
<h3 id="get传值"><a href="#get传值" class="headerlink" title="get传值"></a>get传值</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">app.get(<span class="string">&quot;/product&quot;</span>,<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">let</span> query = req.query;</span><br><span class="line">  <span class="built_in">console</span>.log(query);</span><br><span class="line">  res.send(<span class="string">&quot;product&quot;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">输入：</span><br><span class="line">http:<span class="comment">//localhost:3000/product?id=123&amp;cid=1234</span></span><br><span class="line"></span><br><span class="line">terminal输出：</span><br><span class="line">&#123; <span class="attr">id</span>: <span class="string">&#x27;123&#x27;</span>, <span class="attr">cid</span>: <span class="string">&#x27;1234&#x27;</span> &#125;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="ejs"><a href="#ejs" class="headerlink" title="ejs"></a>ejs</h2><h4 id="安装ejs"><a href="#安装ejs" class="headerlink" title="安装ejs"></a>安装ejs</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">npm i ejs --save</span><br></pre></td></tr></table></figure>
<h4 id="ejs基本使用"><a href="#ejs基本使用" class="headerlink" title="ejs基本使用"></a>ejs基本使用</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> express=<span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> app=express()</span><br><span class="line">app.set(<span class="string">&quot;view engine&quot;</span>,<span class="string">&quot;ejs&quot;</span>)</span><br><span class="line">app.get(<span class="string">&quot;/&quot;</span>,<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">let</span> title = <span class="string">&quot;你好ejs&quot;</span>;</span><br><span class="line">  res.render(<span class="string">&quot;index&quot;</span>,&#123;</span><br><span class="line">    title:title</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.listen(<span class="number">3000</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">//index.ejs</span></span><br><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=<span class="string">&quot;en&quot;</span>&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">  &lt;meta charset=<span class="string">&quot;UTF-8&quot;</span>&gt;</span><br><span class="line">  &lt;meta name=<span class="string">&quot;viewport&quot;</span> content=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span><br><span class="line">  &lt;title&gt;Document&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">  &lt;h2&gt;我是ejs模板&lt;/h2&gt;</span><br><span class="line">  &lt;p&gt;&lt;%=title%&gt;&lt;<span class="regexp">/p&gt;  /</span><span class="regexp">/ejs绑定html数据</span></span><br><span class="line"><span class="regexp">&lt;/</span>body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
<h4 id="ejs-进阶使用"><a href="#ejs-进阶使用" class="headerlink" title="ejs 进阶使用"></a>ejs 进阶使用</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">app.get(<span class="string">&quot;/news&quot;</span>,<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">var</span> userinfo=&#123;</span><br><span class="line">    username:<span class="string">&quot;张三&quot;</span>,</span><br><span class="line">    age:<span class="number">20</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">let</span> article=<span class="string">&quot;&lt;h3&gt;我是一个H3&lt;/h3&gt;&quot;</span> </span><br><span class="line">  res.render(<span class="string">&quot;news&quot;</span>,&#123;</span><br><span class="line">    userinfo:userinfo,</span><br><span class="line">    article:article</span><br><span class="line">    score:<span class="number">60</span>;<span class="comment">//写死的数据</span></span><br><span class="line">  &#125;)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">//news.ejs</span></span><br><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=<span class="string">&quot;en&quot;</span>&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">  &lt;meta charset=<span class="string">&quot;UTF-8&quot;</span>&gt;</span><br><span class="line">  &lt;meta name=<span class="string">&quot;viewport&quot;</span> content=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span><br><span class="line">  &lt;title&gt;Document&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">  &lt;h2&gt;我是一个ejs模板引擎--新闻界面&lt;/h2&gt;</span><br><span class="line">  &lt;h4&gt;&lt;%=userinfo.username  %&gt; ----&lt;%=userinfo.age%&gt;&lt;/h4&gt;</span><br><span class="line">  &lt;p&gt;&lt;%-article%&gt;&lt;/p&gt;     </span><br><span class="line">&lt;!-- 需要解析就不用等号，用- --&gt;</span><br><span class="line"></span><br><span class="line">  &lt;h2&gt;条件判断&lt;/h2&gt;</span><br><span class="line">  &lt;%<span class="keyword">if</span>(score&gt;=<span class="number">60</span>)&#123;%&gt;</span><br><span class="line">    &lt;p&gt;及格&lt;/p&gt;</span><br><span class="line">  &lt;%&#125;<span class="keyword">else</span>&#123;%&gt;</span><br><span class="line">    &lt;p&gt;不及格&lt;/p&gt;</span><br><span class="line">  &lt;%&#125;%&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li>解析html</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;p&gt;&lt;%-article%&gt;&lt;/p&gt;     </span><br></pre></td></tr></table></figure>
<ul>
<li>对象</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;h4&gt;&lt;%=userinfo.username  %&gt; ----&lt;%=userinfo.age%&gt;&lt;/h4&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li>条件判断</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;h2&gt;条件判断&lt;/h2&gt;</span><br><span class="line">&lt;%<span class="keyword">if</span>(score&gt;=<span class="number">60</span>)&#123;%&gt;</span><br><span class="line">  &lt;p&gt;及格&lt;/p&gt;</span><br><span class="line">&lt;%&#125;<span class="keyword">else</span>&#123;%&gt;</span><br><span class="line">  &lt;p&gt;不及格&lt;/p&gt;</span><br><span class="line">&lt;%&#125;%&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li>列表</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;h2&gt;循环遍历&lt;/h2&gt;</span><br><span class="line"> &lt;ul&gt;</span><br><span class="line"> &lt;%<span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="number">0</span>;i&lt;list.length;i++)&#123;%&gt;</span><br><span class="line">   &lt;li&gt;&lt;%=list[i]%&gt;&lt;/li&gt;</span><br><span class="line"> &lt;%&#125;%&gt;</span><br><span class="line"> &lt;/ul&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li>引入其他ejs文件</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;%-include(<span class="string">&#x27;footer.ejs&#x27;</span>)%&gt;</span><br></pre></td></tr></table></figure>
<p>配置静态web目录</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">app.use(express.static(<span class="string">&quot;static&quot;</span>))</span><br><span class="line"></span><br><span class="line">&lt;link rel=<span class="string">&quot;stylesheet&quot;</span> href=<span class="string">&quot;/css/base.css&quot;</span>&gt;</span><br></pre></td></tr></table></figure>
<h2 id="express-中间件"><a href="#express-中间件" class="headerlink" title="express 中间件"></a>express 中间件</h2><h4 id="应用级中间件"><a href="#应用级中间件" class="headerlink" title="应用级中间件"></a>应用级中间件</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//应用级中间件(用于权限判断)</span></span><br><span class="line">app.use(<span class="function">(<span class="params">req,res,next</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="keyword">new</span> <span class="built_in">Date</span>());</span><br><span class="line">    next();</span><br><span class="line">  &#125;)</span><br></pre></td></tr></table></figure>


<h4 id="路由中间件"><a href="#路由中间件" class="headerlink" title="路由中间件"></a>路由中间件</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//路由中间件（不常见）</span></span><br><span class="line">app.get(<span class="string">&quot;/admin/user/add&quot;</span>,<span class="function">(<span class="params">req,res,next</span>)=&gt;</span>&#123; <span class="comment">//get:显示数据</span></span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;多级路由&quot;</span>);</span><br><span class="line">  next();</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.get(<span class="string">&quot;/admin/user/:id&quot;</span>,<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123; <span class="comment">//get:显示数据</span></span><br><span class="line">  <span class="keyword">var</span> id=req.params[<span class="string">&quot;id&quot;</span>];</span><br><span class="line">  res.send(<span class="string">&quot;动态路由+&quot;</span>+id)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h4 id="错误处理中间件"><a href="#错误处理中间件" class="headerlink" title="错误处理中间件"></a>错误处理中间件</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">app.use(<span class="function">(<span class="params">req,res,next</span>)=&gt;</span>&#123;</span><br><span class="line">  res.status(<span class="number">404</span>).send(<span class="string">&quot;404&quot;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h4 id="内置中间件-静态"><a href="#内置中间件-静态" class="headerlink" title="内置中间件(静态)"></a>内置中间件(静态)</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//内置中间件</span></span><br><span class="line">app.use(express.static(<span class="string">&quot;static&quot;</span>))</span><br></pre></td></tr></table></figure>


<h4 id="第三方中间件"><a href="#第三方中间件" class="headerlink" title="第三方中间件"></a>第三方中间件</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">e.g</span><br><span class="line">bodyParser</span><br><span class="line">app.get(<span class="string">&quot;/login&quot;</span>,<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123;</span><br><span class="line">  res.render(<span class="string">&quot;form&quot;</span>,&#123;&#125;)</span><br><span class="line">  </span><br><span class="line">&#125;)</span><br><span class="line">app.post(<span class="string">&quot;/dologin&quot;</span>,<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">var</span> body = req.body;</span><br><span class="line">  <span class="built_in">console</span>.log(body);</span><br><span class="line">  res.send(<span class="string">&quot;执行提交&quot;</span>+body.username)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">//form.ejs</span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;/css/base.css&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">h2</span>&gt;</span>post<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">&quot;/doLogin&quot;</span> <span class="attr">method</span>=<span class="string">&quot;POST&quot;</span>&gt;</span></span><br><span class="line">  用户名:<span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span>&gt;</span></span><br><span class="line">  密码: <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;submit&quot;</span> <span class="attr">value</span>=<span class="string">&quot;提交&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="express-router"><a href="#express-router" class="headerlink" title="express.router"></a>express.router</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> bodyParser = <span class="built_in">require</span>(<span class="string">&#x27;body-parser&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> express=<span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> router= express.Router()</span><br><span class="line"></span><br><span class="line">router.get(<span class="string">&quot;/&quot;</span>,<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123;</span><br><span class="line">  res.render(<span class="string">&quot;form&quot;</span>,&#123;&#125;)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">router.post(<span class="string">&quot;/doLogin&quot;</span>,<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">var</span> body=req.body;</span><br><span class="line">  res.send(<span class="string">&quot;执行提交&quot;</span>+body.username)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = router</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//app.js</span></span><br><span class="line"><span class="comment">//引入login</span></span><br><span class="line"><span class="keyword">const</span> login = <span class="built_in">require</span>(<span class="string">&quot;./router/login&quot;</span>)</span><br><span class="line"><span class="comment">//挂载login模块</span></span><br><span class="line">app.use(<span class="string">&quot;/login&quot;</span>,login)</span><br></pre></td></tr></table></figure>


<h2 id="express和mongoose"><a href="#express和mongoose" class="headerlink" title="express和mongoose"></a>express和mongoose</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> mongoose = <span class="built_in">require</span>(<span class="string">&#x27;mongoose&#x27;</span>)</span><br><span class="line"></span><br><span class="line">mongoose.connect(<span class="string">&#x27;mongodb://127.0.0.1:27017/users&#x27;</span>)</span><br><span class="line"><span class="comment">//定义一个Schema Schema里面对象和数据库里面字段要一一对应</span></span><br><span class="line"><span class="keyword">var</span> UserSchema=mongoose.Schema(&#123;</span><br><span class="line">  name:<span class="built_in">String</span>,</span><br><span class="line">  phone:<span class="built_in">Number</span>,</span><br><span class="line">  status:<span class="built_in">Number</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//model里第一个参数会自动和数据库的复数对应起来</span></span><br><span class="line"><span class="keyword">var</span> User=mongoose.model(<span class="string">&#x27;User&#x27;</span>,UserSchema);</span><br></pre></td></tr></table></figure>
<h4 id="查询users表的数据"><a href="#查询users表的数据" class="headerlink" title="查询users表的数据"></a>查询users表的数据</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//查询users表的数据</span></span><br><span class="line"></span><br><span class="line">User.find(&#123;&#125;,<span class="function"><span class="keyword">function</span>(<span class="params">err,doc</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="keyword">return</span> ;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(doc);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="增加数据-1"><a href="#增加数据-1" class="headerlink" title="增加数据"></a>增加数据</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//增加数据</span></span><br><span class="line"><span class="comment">//通过实例化Model 创建增加的数据</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> u=<span class="keyword">new</span> User(&#123;</span><br><span class="line">  name:<span class="string">&#x27;李四&#x27;</span>,</span><br><span class="line">  age:<span class="number">20</span>,</span><br><span class="line">  status:<span class="number">1</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">u.save(<span class="function"><span class="keyword">function</span>(<span class="params">err</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="keyword">return</span> ;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;success&quot;</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="更新数据"><a href="#更新数据" class="headerlink" title="更新数据"></a>更新数据</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//更新数据</span></span><br><span class="line"></span><br><span class="line">User.updateOne(&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;李四&quot;</span>&#125;,&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;吴明泽&quot;</span>&#125;,<span class="function"><span class="keyword">function</span>(<span class="params">err,doc</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">console</span>.log(err);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;成功&quot;</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="删除数据-1"><a href="#删除数据-1" class="headerlink" title="删除数据"></a>删除数据</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//删除数据</span></span><br><span class="line"></span><br><span class="line">User.deleteOne(&#123;<span class="string">&quot;age&quot;</span>:&#123;<span class="attr">$gte</span>:<span class="number">2</span>&#125;&#125;,<span class="function"><span class="keyword">function</span>(<span class="params">err,res</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">console</span>.log(err);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;成功&quot;</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h3 id="模块化"><a href="#模块化" class="headerlink" title="模块化"></a>模块化</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//app.js</span></span><br><span class="line"><span class="keyword">var</span> UserModel=<span class="built_in">require</span>(<span class="string">&#x27;./user&#x27;</span>);</span><br><span class="line"></span><br><span class="line">UserModel.find(&#123;&#125;,<span class="function"><span class="keyword">function</span>(<span class="params">err,doc</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="keyword">return</span> ;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(doc);</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">//db.js  用来连接数据库</span></span><br><span class="line"><span class="keyword">const</span> mongoose = <span class="built_in">require</span>(<span class="string">&#x27;mongoose&#x27;</span>)</span><br><span class="line"></span><br><span class="line">mongoose.connect(<span class="string">&#x27;mongodb://127.0.0.1:27017/users&#x27;</span>,&#123;<span class="attr">useNewUrlParser</span>:<span class="literal">true</span>&#125;,<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;数据库连接成功&quot;</span>);</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//定义一个Schema Schema里面对象和数据库里面字段要一一对应</span></span><br><span class="line"><span class="built_in">module</span>.exports=mongoose;</span><br><span class="line"></span><br><span class="line"><span class="comment">//user.js 配置Schema</span></span><br><span class="line"><span class="keyword">var</span> mongoose=<span class="built_in">require</span>(<span class="string">&#x27;./db.js&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> UserSchema=mongoose.Schema(&#123;</span><br><span class="line">  name:<span class="built_in">String</span>,</span><br><span class="line">  phone:<span class="built_in">Number</span>,</span><br><span class="line">  status:<span class="built_in">Number</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> UserModel=mongoose.model(<span class="string">&#x27;user&#x27;</span>,UserSchema);</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports=UserModel;</span><br></pre></td></tr></table></figure>


<h2 id="Nginx配置"><a href="#Nginx配置" class="headerlink" title="Nginx配置"></a>Nginx配置</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">#user  nobody;</span><br><span class="line">worker_processes  <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">#error_log  logs/error.log;</span><br><span class="line">#error_log  logs/error.log  notice;</span><br><span class="line">#error_log  logs/error.log  info;</span><br><span class="line"></span><br><span class="line">#pid        logs/nginx.pid;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">events &#123;</span><br><span class="line">    worker_connections  <span class="number">1024</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">http &#123;</span><br><span class="line">    include       mime.types;</span><br><span class="line">    default_type  application/octet-stream;</span><br><span class="line"></span><br><span class="line">    #log_format  main  <span class="string">&#x27;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &#x27;</span></span><br><span class="line">    #                  &#x27;$status $body_bytes_sent &quot;$http_referer&quot; &#x27;</span><br><span class="line">    #                  &#x27;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;&#x27;;</span><br><span class="line"></span><br><span class="line">    #access_log  logs/access.log  main;</span><br><span class="line"></span><br><span class="line">    sendfile        on;</span><br><span class="line">    #tcp_nopush     on;</span><br><span class="line"></span><br><span class="line">    #keepalive_timeout  <span class="number">0</span>;</span><br><span class="line">    keepalive_timeout  <span class="number">65</span>;</span><br><span class="line"></span><br><span class="line">    #gzip  on;</span><br><span class="line"></span><br><span class="line">    server &#123;</span><br><span class="line">        listen       <span class="number">9999</span>;</span><br><span class="line">        server_name  <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>;</span><br><span class="line"></span><br><span class="line">        #charset koi8-r;</span><br><span class="line"></span><br><span class="line">        #access_log  logs/host.access.log  main;</span><br><span class="line"></span><br><span class="line">        location / &#123;</span><br><span class="line">             root   html;            </span><br><span class="line">		try_files $uri $uri/ /index.html;             </span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        location /apis/&#123;</span><br><span class="line">    	      proxy_pass	http:<span class="comment">//127.0.0.1:3000/; # 将地址代理到api上</span></span><br><span class="line">   	 &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        #error_page  <span class="number">404</span>              /<span class="number">404.</span>html;</span><br><span class="line"></span><br><span class="line">        # redirect server error pages to the static page /50x.html</span><br><span class="line">        #</span><br><span class="line">        error_page   <span class="number">500</span> <span class="number">502</span> <span class="number">503</span> <span class="number">504</span>  /50x.html;</span><br><span class="line">        location = /50x.html &#123;</span><br><span class="line">            root   html;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        # proxy the PHP scripts to Apache listening on 127.0.0.1:80</span><br><span class="line">        #</span><br><span class="line">        #location ~ \.php$ &#123;</span><br><span class="line">        #    proxy_pass   http://127.0.0.1;</span><br><span class="line">        #&#125;</span><br><span class="line"></span><br><span class="line">        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</span><br><span class="line">        #</span><br><span class="line">        #location ~ \.php$ &#123;</span><br><span class="line">        #    root           html;</span><br><span class="line">        #    fastcgi_pass   127.0.0.1:9000;</span><br><span class="line">        #    fastcgi_index  index.php;</span><br><span class="line">        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;</span><br><span class="line">        #    include        fastcgi_params;</span><br><span class="line">        #&#125;</span><br><span class="line"></span><br><span class="line">        # deny access to .htaccess files, if Apache&#x27;s document root</span><br><span class="line">        # concurs with nginx&#x27;s one</span><br><span class="line">        #</span><br><span class="line">        #location ~ /\.ht &#123;</span><br><span class="line">        #    deny  all;</span><br><span class="line">        #&#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    # another virtual host using mix of IP-, name-, and port-based configuration</span><br><span class="line">    #</span><br><span class="line">    #server &#123;</span><br><span class="line">    #    listen       8000;</span><br><span class="line">    #    listen       somename:8080;</span><br><span class="line">    #    server_name  somename  alias  another.alias;</span><br><span class="line"></span><br><span class="line">    #    location / &#123;</span><br><span class="line">    #        root   html;</span><br><span class="line">    #        index  index.html index.htm;</span><br><span class="line">    #    &#125;</span><br><span class="line">    #&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    # HTTPS server</span><br><span class="line">    #</span><br><span class="line">    #server &#123;</span><br><span class="line">    #    listen       443 ssl;</span><br><span class="line">    #    server_name  localhost;</span><br><span class="line"></span><br><span class="line">    #    ssl_certificate      cert.pem;</span><br><span class="line">    #    ssl_certificate_key  cert.key;</span><br><span class="line"></span><br><span class="line">    #    ssl_session_cache    shared:SSL:1m;</span><br><span class="line">    #    ssl_session_timeout  5m;</span><br><span class="line"></span><br><span class="line">    #    ssl_ciphers  HIGH:!aNULL:!MD5;</span><br><span class="line">    #    ssl_prefer_server_ciphers  on;</span><br><span class="line"></span><br><span class="line">    #    location / &#123;</span><br><span class="line">    #        root   html;</span><br><span class="line">    #        index  index.html index.htm;</span><br><span class="line">    #    &#125;</span><br><span class="line">    #&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>





































    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E6%8A%80%E6%9C%AF/" rel="tag"># 技术</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
            </div>
            <div class="post-nav-item">
                <a href="/2021/01/25/vue%E6%96%87%E6%A1%A3%E6%80%BB%E7%BB%93/" rel="next" title="Vue 文档总结">
                  Vue 文档总结 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Lawted Wu</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  


  <script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>


<script>
var options = {
  bottom: '64px', // default: '32px'
  right: '32px', // default: '32px'
  left: 'unset', // default: 'unset'
  time: '0.5s', // default: '0.3s'
  mixColor: '#fff', // default: '#fff'
  backgroundColor: '#fff',  // default: '#fff'
  buttonColorDark: '#000',  // default: '#100f2c'
  buttonColorLight: '#fff', // default: '#fff'
  saveInCookies: true, // default: true,
  label: ' ', // default: ''
  autoMatchOsTheme: true // default: true
}
const darkmode = new Darkmode(options);
darkmode.showWidget();
</script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  






  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
